package com.lxb.eduservice.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxb.commonutils.JwtUtils;
import com.lxb.commonutils.R;
import com.lxb.commonutils.vo.UcenterMemberVO;
import com.lxb.eduservice.client.UcenterClient;
import com.lxb.eduservice.entity.EduComment;
import com.lxb.eduservice.service.EduCommentService;
import com.lxb.eduservice.vo.front.EduCommentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论功能模块控制层
 *
 * @author lixuebin
 * @since 2023-01-11
 */
@Api(description = "评论功能模块控制层")
@RestController
@RequestMapping("/eduservice/eduComment")
//@CrossOrigin
public class EduCommentController {

    @Autowired
    private EduCommentService commentService;
    @Autowired
    private UcenterClient ucenterClient;

    @ApiOperation("评论列表分页")
    @GetMapping("/getCommentPage/{page}/{limit}")
    public R getCommentPage(@PathVariable long page, @PathVariable long limit, String courseId) {

        //分页
        Page<EduComment> eduCommentPage = new Page<>(page, limit);

        //分页条件
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        //根据课程id查询，评论数据
        wrapper.eq("course_id", courseId);
        //查询一级评论
        wrapper.eq("pid", "0");
        //按照插入时间，降序
        wrapper.orderByDesc("gmt_create");

        commentService.page(eduCommentPage, wrapper);

        //存放分页数据
        Map<String, Object> map = new HashMap<>();

        List<EduComment> records = eduCommentPage.getRecords();
        map.put("list", records);//每页数据list集合

        List<EduCommentVo> eduCommentVoList = new ArrayList<>();
        //TODO 判断数据是否有回复
        if (records != null) {
            //判断
            for (EduComment list : records) {
                //获取一级 二级 评论
                EduCommentVo commentVos = commentService.callBack(list.getId());
                eduCommentVoList.add(commentVos);
            }
            map.put("list", eduCommentVoList);//每页数据list集合
        }

        map.put("total", eduCommentPage.getTotal());//总记录数
        map.put("pages", eduCommentPage.getPages());//总页数
        map.put("current", eduCommentPage.getCurrent());//当前页
        map.put("size", eduCommentPage.getSize());//每页记录条数
        map.put("hasNext", eduCommentPage.hasNext());//下一页
        map.put("hasPrevious", eduCommentPage.hasPrevious());//上一页

        return R.ok().data(map);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("/addComment")
    public R addComment(@RequestBody EduComment comment, HttpServletRequest request) {

        //1.传入请求数据，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        //判断用户token是否可以获取id
        if (StringUtils.isEmpty(memberId)) {
            return R.error().code(28004).message("评论失败，请先登录账号");
        }
        //设置默认一级评论 pid 为 0 默认值，标识为一级id
        if (comment.getPid() == null) {
            comment.setPid("0");
        }
        //调用 service_ucenter服务 获取用户信息
        UcenterMemberVO userInfo = ucenterClient.getInfoUc(memberId);

        comment.setMemberId(memberId);//会员id
        comment.setNickname(userInfo.getNickname());//会员昵称
        comment.setAvatar(userInfo.getAvatar());//会员头像

        //提交评论数据
        commentService.save(comment);

        return R.ok();
    }
}

